﻿using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace demoMVVM
{
    public class MainVM: BindableBase
    {

        public DelegateCommand OkCommand { get; set; }

        public MainVM()
        {
            OkCommand = new DelegateCommand(() => {
                if (!IsValidated)
                {
                    MessageBox.Show("处理下错误信息");

                    return;        
                }
            });
        }


        private bool isValidated = true; // 默认为全部通过验证
        public bool IsValidated
        {
            get { return isValidated; }
            protected set { isValidated = value; }
        }



        private string name;
        private double age;

        /// <summary>
        /// 姓名
        /// </summary>
        public string Name
        {
            get { return name; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    name = value;
                    RaisePropertyChanged();
                }
                else
                {
                    IsValidated = false;
                    throw new Exception("Age should between 0 and 150");
                }
            }
        }


        /// <summary>
        /// 年龄
        /// </summary>
        public double Age
        {
            get { return age; }
            set
            {
                if (value >= 0 && value <= 150)
                {
                    age = value;
                    RaisePropertyChanged();
                }
                else
                {
                    IsValidated = false;
                    throw new Exception("Age should between 0 and 150");
                }
            }
        }


    }
}
